import { OrgServiceApi } from "./_org";
import { ToolServiceApi } from "./_tool";
import { AxiosResponse } from "axios";
import { objectToUrlParams } from "@/utils/common";
import service from "@/api";
import { SYSTEM_CONFIG } from "@/config/system";
import { SERVICE_CONFIG } from "../_base";
import type {
  JxtechPageResult,
  AttachmentQueryParm,
  FileAttachmentQueryParm,
  FolderAttachmentQueryParm,
  DeleteFileParam,
  SaveUploadFile,
  InsertFolderParm
} from "@whjxtech/jxtech-component";

/**上传文件接口 */
export const saveUploadFileUrl = SYSTEM_CONFIG.BASE_URL + SERVICE_CONFIG.ORG + "/org/FileAttachmentService/saveUploadFile";
/**下载文件接口 */
export const downloadFileUrl = SYSTEM_CONFIG.BASE_URL + SERVICE_CONFIG.ORG + "/org/FileAttachmentService/downloadFile?fileId=";

/**下载文件接口 */
export function downloadFile(fileId: string) {
  let url = downloadFileUrl + fileId;
  return service.get(url, { responseType: "blob", timeout: SYSTEM_CONFIG.DOWNLOAD_FILE_TIMEOUT });
}

/**上传文件接口 */
export function uploadFile(formData: FormData, setProgress?: Function, fileSize?: number) {
  return service.post(saveUploadFileUrl, formData, {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    onUploadProgress: function (progressEvent) {
      if (setProgress && fileSize) setProgress(progressEvent.loaded, fileSize);
    }
  });
}
class FilesQueryExtServiceApi extends ToolServiceApi {
  /**查询全部附件 */
  attachmentLoadData(bean: AttachmentQueryParm, params?: any): Promise<AxiosResponse<JxtechPageResult>> {
    let url = this.baseUrl + "/pageQueryAll";
    url += objectToUrlParams(params, true);
    return service.get(url, { params: { ...bean } });
  }
}
export const ExportFilesQueryExtServiceApi = new FilesQueryExtServiceApi("/FilesQueryExtService");

class FileAttachmentServiceApi extends OrgServiceApi {
  /**查询附件详情 */
  attachmentQueryFileHandler(bean: { pk: string }, params?: any): Promise<AxiosResponse<FileAttachmentQueryParm>> {
    let url = this.baseUrl + "/queryById";
    url += objectToUrlParams(params, true);
    return service.get(url, { params: { ...bean } });
  }
  /**修改保存文件 */
  attachmentSaveUpdateFileHandler(bean: FileAttachmentQueryParm, params?: any): Promise<AxiosResponse<FileAttachmentQueryParm>> {
    let url = this.baseUrl + "/saveUpdate";
    url += objectToUrlParams(params, true);
    return service.post(url, bean);
  }
}
export const ExportFileAttachmentServiceApi = new FileAttachmentServiceApi("/FileAttachmentService");

class FileTypeServiceApi extends ToolServiceApi {
  /**查询文件夹详情 */
  attachmentQueryFolderHandler(bean: { pk: string }, params?: any): Promise<AxiosResponse<FolderAttachmentQueryParm>> {
    let url = this.baseUrl + "/queryById";
    url += objectToUrlParams(params, true);
    return service.get(url, { params: { ...bean } });
  }
  /**修改保存文件夹 */
  attachmentSaveUpdateFolderHandler(
    bean: FolderAttachmentQueryParm,
    params?: any
  ): Promise<AxiosResponse<FolderAttachmentQueryParm>> {
    let url = this.baseUrl + "/saveUpdate";
    url += objectToUrlParams(params, true);
    return service.post(url, bean);
  }
  /**新建文件夹方法 */
  attachmentInsertFolder(bean: InsertFolderParm, params?: any): Promise<AxiosResponse<FolderAttachmentQueryParm>> {
    let url = this.baseUrl + "/saveInsert";
    url += objectToUrlParams(params, true);
    return service.post(url, bean);
  }
}
export const ExportFileTypeServiceApi = new FileTypeServiceApi("/FileTypeService");

class FilesServiceApi extends ToolServiceApi {
  /**删除文件 */
  attachmentDeleteFile(bean: DeleteFileParam[], params?: any): Promise<AxiosResponse<any>> {
    let url = this.baseUrl + "/delFiles";
    url += objectToUrlParams(params, true);
    return service.post(url, bean);
  }
  /**保存附件方法 */
  attachmentSaveMulitFiles(bean: { insert: SaveUploadFile[] }, params?: any): Promise<AxiosResponse<any>> {
    let url = this.baseUrl + "/saveMulti";
    url += objectToUrlParams(params, true);
    return service.post(url, bean);
  }
}
export const ExportFilesServiceApi = new FilesServiceApi("/FilesService");
